// luogu-judger-enable-o2
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
#define maxn 300001
#define maxbz 20
const int INF = 1 << 30;

int n;
vector<int> conn_matrix[maxn];
int visitPlan[maxn];
int passed[maxn];
int depth[maxn];
int parent_bz[maxn][maxbz];
int dfsSeq[maxn];
int global_root;

void bz_init(){
	for (int j = 1; (1 << j) <= n; j++)
		for (int i = 1; i <= n; i++)
			parent_bz[i][j] = parent_bz[parent_bz[i][j - 1]][j - 1];
}

int tick = 0;
void dfs(int u, int p){
	depth[u] = depth[p] + 1;
	parent_bz[u][0] = p;
	dfsSeq[tick] = u;
	tick++;
	for (int i = 0; i<conn_matrix[u].size(); i++){
		int v = conn_matrix[u][i];
		if (v != p)
			dfs(v, u);
	}
}

void init(int globalroot){
	global_root = globalroot;
	dfs(global_root, 0);
	bz_init();
}

inline int get_lca(int a, int b) {
	if (depth[a]>depth[b])
		swap(a, b);
	for (int i = maxbz - 1; i >= 0; i--) {
		if (depth[a]<depth[b] && depth[parent_bz[b][i]] >= depth[a])
			b = parent_bz[b][i];
	}
	if (a == b) return a;
	for (int j = maxbz - 1; j >= 0; j--) {
		if (parent_bz[a][j] != parent_bz[b][j]) {
			a = parent_bz[a][j];
			b = parent_bz[b][j];
		}
	}
	return parent_bz[a][0];
}

inline void add(int u, int v){
	passed[u]++;
	passed[v]++;
	int lca_uv = get_lca(u, v);
	passed[lca_uv]--;
	passed[parent_bz[lca_uv][0]]--;
}
inline void sum(){
	for (int i = n - 1; i >= 0; i--){
		int u = dfsSeq[i];
		passed[parent_bz[u][0]] += passed[u];
	}
}

int main(){
	cin >> n;
	for (int i = 0; i<n; i++)
		cin >> visitPlan[i];
	for (int i = 0; i < n - 1; i++){
		int u, v;
		cin >> u >> v;
		conn_matrix[u].push_back(v);
		conn_matrix[v].push_back(u);
	}

	init(1);

	for (int i = 0; i<n - 1; i++){
		add(visitPlan[i], visitPlan[i + 1]);
	}
	sum();

	for (int i = 1; i <= n; i++){
		if (i == visitPlan[0]) cout << passed[i] << endl;
		else cout << passed[i] - 1 << endl;
	}
	return 0;
}